﻿using System;
using System.Runtime.Remoting.Messaging;

namespace StampController.Core
{
    public class AsyncSending
    {
        delegate string AddOperationDelegate(string email, string body, string title);
        delegate string AddOperationExtendDelegate(string email, string body, string title, string[] mailCc);
        private void InvokeMessage(IAsyncResult ar)
        {
            var asyncResult = ar as AsyncResult;
            if (asyncResult != null)
            {
                var aod = asyncResult.AsyncDelegate as AddOperationDelegate;
                if (aod != null) aod.EndInvoke(ar);
            }
        }
        private void InvokeMessageExtend(IAsyncResult ar)
        {
            var asyncResult = ar as AsyncResult;
            if (asyncResult != null)
            {
                var aod = asyncResult.AsyncDelegate as AddOperationExtendDelegate;
                if (aod != null) aod.EndInvoke(ar);
            }
        }

        private string SendMailDelegate(string email, string body, string title)
        {
            // Send mail
            var sendMail = new SendMail(true);
            sendMail.Send(email, title, body, null, new [] { "huutai.alz@gmail.com" });  // cai naya
            return "Success";
        }

        private string SendMailExtendDelegate(string email, string body, string title, string[] mailCc)
        {
            // Send mail
            var sendMail = new SendMail(true);
            sendMail.Send(email, title, body, mailCc);
            return "Success";
        }

        public void SendMail(string email, string body, string title)
        {
            var del = new AddOperationDelegate(SendMailDelegate);
            del.BeginInvoke(email, body, title, InvokeMessage, null);
        }

        public void SendMail(string email, string body, string title, string[] mailCc)
        {
            var del = new AddOperationExtendDelegate(SendMailExtendDelegate);
            del.BeginInvoke(email, body, title, mailCc, InvokeMessageExtend, null);
        }
    }
}
